import { modifyCompanyInfoApi } from "@/api/managementCenter/departmentPersonnel";
import { addDepartApi } from "@/api/managementCenter/departmentPersonnel";
import FormHook from "@/hook/formHook";
import { message } from "ant-design-vue";
import { Ref } from "vue";
export default function OperationHook(state: { formState: DepartmentPersonnel.CompanyInfoForm; formDataRef: Ref<any>; emit: any; passData: Base.GeneraDataType; type: Ref<number> }) {
  const { formState, formDataRef, emit, passData, type } = state;
  const { resetFields, validateZhInputHandle, validateEnHandle, validateIntergeHandle } = FormHook({ formState });
  const submitHandle = () => {
    formState.bizDeptApiConfigReqDtoList.map((t: DepartmentPersonnel.ApiInfo, index: number) => {
      if (index !== 0 && !t.agent && t.agentId && !t.apiKey && !t.domainName && !t.platformId && !t.platformName && !t.userName) {
        formState.bizDeptApiConfigReqDtoList.splice(index, 1);
      }
    });
    formDataRef.value.validate().then(() => {
      const params: any = {
        ...formState,
        // bizDeptAccountReqDtoList: formState.bizDeptAccountReqDtoList.map((t: DepartmentPersonnel.AccountInfo) => {
        //   return {
        //     ...t,
        //     deptId: passData.value.id
        //   };
        // }),
        logUrl: formState.logUrls[0],
        bizDeptAccountReqDtoList: undefined
      };

      let requestFn;

      if (type.value === 1) {
        requestFn = modifyCompanyInfoApi;
      } else {
        requestFn = addDepartApi;
        params.id = passData.value.id;
        params.pid = passData.value.pid;
      }
      requestFn(params).then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功!");
          emit("submit");
          emit("updateInfo");
          resetFields();
          formDataRef.value.clearValidate();
        }
      });
    });
  };
  const resetHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };
  const changeAddressHandle = (value: any, selectedOptions: any) => {
    const name = selectedOptions.map((t: any) => t.name);
    const code = selectedOptions.map((t: any) => t.code);
    if (selectedOptions.length) {
      formState.provinceName = name[0];
      formState.cityName = name[1];
      formState.areaName = name[2];
      formState.provinceCode = code[0];
      formState.cityCode = code[1];
      formState.areaCode = code[2];
    }
  };
  // const addAccountHandle = () => {
  //   formState.bizDeptAccountReqDtoList.push({
  //     name: "",
  //     bankName: "",
  //     receiveAccount: "",
  //     isDefault: 0,
  //     sisterBankNum: ""
  //   });
  // };
  // const deleteAccountHandle = (index: number) => {
  //   formState.bizDeptAccountReqDtoList.splice(index, 1);
  // };
  const addAgentConfigHandle = () => {
    formState.bizDeptApiConfigReqDtoList.push({
      platformId: "",
      platformName: "",
      agentId: "",
      agent: "",
      apiKey: "",
      domainName: "",
      userName: ""
    });
  };
  const deleteAgentConfigHandle = (index: number) => {
    formState.bizDeptApiConfigReqDtoList.splice(index, 1);
  };
  // const changeDefaultAccountHandle = (index: number) => {
  //   if (formState.bizDeptAccountReqDtoList[index].isDefault === 1) {
  //     formState.bizDeptAccountReqDtoList.forEach((item: DepartmentPersonnel.AccountInfo) => (item.isDefault = 0));
  //     formState.bizDeptAccountReqDtoList[index].isDefault = 1;
  //   }
  // };
  return {
    validateZhInputHandle,
    submitHandle,
    resetHandle,
    validateEnHandle,
    validateIntergeHandle,
    changeAddressHandle,
    // addAccountHandle,
    // deleteAccountHandle,
    // changeDefaultAccountHandle,
    addAgentConfigHandle,
    deleteAgentConfigHandle,
    resetFields
  };
}
